using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using System.Globalization;

namespace Camalon.Drawing
{
	public class CornerConverter:ExpandableInstanceConverter
	{

		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			try
			{
				if ( value is string ) 
				{
				

					string sVal = (string)value;
					if( sVal.Length == 0) return Corner.Empty;

					sVal = sVal.Trim();
					if( sVal.ToLower().IndexOf("empty") != -1) return Corner.Empty;
			
					string[] args = sVal.Split(';');
					if( args.Length == 2) 
					{
				
						char[] percentChar = culture.NumberFormat.PercentSymbol.ToCharArray();
						string width  = args[0].Trim();	string[] x_ = width.Split(percentChar);	width = x_[0];
						string height  = args[1].Trim();	string[] y_ = height.Split(percentChar);	height = y_[0];

						return new Corner( float.Parse( width )/100f,float.Parse( height )/100f);
					}
					else
						if( args.Length == 3) 
					{
				
						char[] percentChar = culture.NumberFormat.PercentSymbol.ToCharArray();
						string width  = args[0].Trim();	string[] x_ = width.Split(percentChar);	width = x_[0];
						string height  = args[1].Trim();	string[] y_ = height.Split(percentChar);	height = y_[0];
						CornerStyle style = (CornerStyle) Enum.Parse(typeof(CornerStyle), args[2], true);

						return new Corner( float.Parse( width )/100f,float.Parse( height )/100f, style);
					}

				}

				//return context.PropertyDescriptor.GetValue( context.Instance );
				return base.ConvertFrom (context, culture, value);
			}
			catch(Exception e)
			{
				FileWatcher.WriteLine( "Corner:ConvertFrom -->" + e.Message +"\r\n"+e.StackTrace);
				throw e;
			}
		}

		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
		{
			try
			{
				//if( value is Camalon.Drawing.Corner )
                if (value.GetType().ToString() == "Camalon.Drawing.Corner")
                {

					Corner m = (Corner) value;

					if (destinationType == typeof(string)) 
					{
                        if (m.IsEmpty) return "Empty";
						
						if( m.Style== CornerStyle.Elliptic)
						{
							string percent = culture.NumberFormat.PercentSymbol;
							float w = (float)System.Math.Round( m.Width*100f, 1);
							float h = (float)System.Math.Round( m.Height*100f, 1);
							return String.Format( "{0}{2}; {1}{2}; {3}", w,h, percent, m.Style);
							//return String.Format( "{0:0.0}{2}; {1:0.0}{2}", m.Width*100f,m.Height*100f, percent);
						}
						else
						{
							string percent = culture.NumberFormat.PercentSymbol;
							float w = (float)System.Math.Round( m.Width*100f, 1);
							float h = (float)System.Math.Round( m.Height*100f, 1);
							return String.Format( "{0}{2}; {1}{2}; {3}", w,h, percent, m.Style);
							//return String.Format( "{0:0.0}{2}; {1:0.0}{2}; {3}", m.Width*100f,m.Height*100f, percent, m.Style);
						}
					}

					if (destinationType == typeof(InstanceDescriptor) ) 
					{

                        if (m.Style == CornerStyle.Elliptic)
                        {
                            ConstructorInfo ctor = typeof(Corner).GetConstructor(new Type[] { typeof(float), typeof(float) });
                            if (ctor != null) return new InstanceDescriptor(ctor, new object[] { m.Width, m.Height });
                        }
                        else
						{
							ConstructorInfo ctor = typeof(Corner).GetConstructor(new Type[] {typeof(float), typeof(float), typeof(CornerStyle)});
							if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.Width,m.Height, m.Style});
                       
                        }
                    
                    }

				}


                return base.ConvertTo(context, culture, value, destinationType);
			}
			catch( Exception e)
			{
				FileWatcher.WriteLine( "Corner:ConvertTo -->" + e.Message +"\r\n"+e.StackTrace);
				throw e;
			}
		}


	}

}
